const app = getApp();
//api
const Api = require("../../api/method.js");
Component({
  properties: {
    islogin:{
      type:Boolean,
      vlaue:false
    },
    isValid:{
      type: Boolean,
      vlaue: false
    },
    hxId:{
      type: String,
      vlaue: "1"
    },
    name:{
      type:String,
      value:"1"
    },
  },

  data: {
    isAuthor: false, //是否已授权
    encryptedData:"",
    iv:""
  },

  methods: {
    //已登陆
    goOn() {
      this.triggerEvent('flagEvent', {});
    },
    //授权
    bindGetUserInfo(res) {
      let that = this;
      if (res.detail.userInfo) {
        console.log("同意授权");
        that.setData({
          encryptedData: res.detail.encryptedData,
          iv: res.detail.iv
        }),
        that.getLoginInfo();
      } else {
        console.log("用户拒绝授权")
      }
    },
    //获取登陆信息
    getLoginInfo() {
      let that = this;
      wx.login({
        success:function(data) {
          if(wx.getStorageSync("url")){
            if(wx.getStorageSync("url").rc == null){
              var list = {
                code: data.code,
                encryptedData: that.data.encryptedData,
                iv: that.data.iv,
                // rc: wx.getStorageSync("url").rc,
                ds: wx.getStorageSync("url").ds,
                branchId: wx.getStorageSync("url").branchId,
              }
            } else if(wx.getStorageSync("url").ds == null){
              var list = {
                code: data.code,
                encryptedData: that.data.encryptedData,
                iv: that.data.iv,
                rc: wx.getStorageSync("url").rc,
                // ds: wx.getStorageSync("url").ds,
                branchId: wx.getStorageSync("url").branchId,
              }
            } else if(wx.getStorageSync("url").branchId == null){
              var list = {
                code: data.code,
                encryptedData: that.data.encryptedData,
                iv: that.data.iv,
                rc: wx.getStorageSync("url").rc,
                ds: wx.getStorageSync("url").ds,
                // branchId: wx.getStorageSync("url").branchId,
              }
            }else{
              var list = {
                code: data.code,
                encryptedData: that.data.encryptedData,
                iv: that.data.iv,
                rc: wx.getStorageSync("url").rc,
                ds: wx.getStorageSync("url").ds,
                branchId: wx.getStorageSync("url").branchId,
              }
            }
          }else{
            var list = {
              code: data.code,
              encryptedData: that.data.encryptedData,
              iv: that.data.iv
            }
          };
          if (data.code) {
            console.log(list);
            //授权调取
            Api.signcode(list, function (err, res) {
              console.log(res.data);
              if(res.data.code == 1){
                let token = res.data.data.token;
                let user = res.data.data.user;
                wx.setStorageSync('token', token);
                wx.setStorageSync("user", user);
                that.isLogin();
                if(res.data.data.user.roleCode == "salesman"){
                  wx.redirectTo({
                    url: '/pages/Salesman/curriculum/curriculum',
                  })
                } else if (res.data.data.user.roleCode == "user"){
                  wx.redirectTo({
                    url: '/pages/User/homepage/homepage',
                  })
                }
              }
            })
          }
        }
      })
    },
    //登陆 
    isLogin() {
      let that = this;
      that.setData({
          isAuthor: true
      });
      wx.setStorageSync("isAuthor" , that.data.isAuthor);
      that.triggerEvent('flagEvent', {});
    },
  }
})